package app.server.project.main.service.service.rank;

import app.server.project.main.service.db.dao.user.UserRankDao;
import app.server.project.main.service.db.entity.cfg.RankCfgEntity;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Component
@Slf4j
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DayRank extends RankBase {

    @Autowired
    private UserRankDao userRankDao;
    @Autowired
    private TaskScheduler taskScheduler;


    @Override
    public void flushNextTime(RankCfgEntity cfgEntity) {
        var now = new Date();
        var zeroTime = DateUtil.beginOfDay(now);
        var time17 = DateUtil.offsetHour(zeroTime, 19);
        if (time17.isBefore(now)) {
            //现在的时间已经过去了,+1天
            time17 = DateUtil.offsetDay(time17, 1);
        }
        cfgEntity.setCreateTime(LocalDateTime.now());
        cfgEntity.setNextSettlementTime(time17);
    }

    @Override
    public void doSettlement() {
        this.userRankDao.reset(this.getRankId());
    }
}
